﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YXChatApiService.Core.Common;
using YXChatApiService.Repository;

namespace YXChatApiService.Core.ChatRecordEntity.DomainManager
{
    public class ChatRecordManager : DomainBaseManager<ChatRecord, Guid>, IChatRecordManager
    {
        public ChatRecordManager(IRepository<ChatRecord, Guid> repository) : base(repository)
        {
        }

        public List<ChatRecord> GetBuddyAll(string selfNumber, string buddyNumber)
        {
            var selfSendMsg = QueryAsNoTracking.Where(x => x.SNumber == selfNumber && x.RNumber == buddyNumber).ToList();
            var buddySendMsg = QueryAsNoTracking.Where(x => x.SNumber == buddyNumber && x.RNumber == selfNumber).ToList();
            List<ChatRecord> chats = selfSendMsg.Concat(buddySendMsg).OrderByDescending(n => n.CreateTime)
                .ToList();
            return chats;
        }

        public async Task<ChatRecord> InsertAsync(ChatRecord entity)
        {
            return await Repo.InsertAsync(entity);
        }
    }
}
